<?php
	// start output buffering
	ob_start();
?>

<?php
	class GetSLT {
	
		var $username;
		var $siteId;
		var $sslcert;
		var $sslkey;
		var $cainfo;		
		
		public function Request($_username, $_loginSeconds) {
			
			$passportUrl = "https://ppsacredential.service.passport.net/pksecure/PPSACredentialPK.srf";
			$exchangeLabsUrl = "https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=10&ct=" . urlencode(time()) . "1217534617rver=5.5.4177.0&wp=MBI_SSL&wreply=https:%2F%2Fexchangelabs.com%2Fowa%2F&lc=1033";
					
			$slt = '';
										
			$data =         "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
			$data = $data . "   <soap:Header>";
			$data = $data . "     <WSSecurityHeader xmlns=\"http://schemas.microsoft.com/Passport/SoapServices/CredentialServiceAPI/V1\">";
			$data = $data . "         <version>eshHeader25</version>";
			$data = $data . "         <ppSoapHeader25><![CDATA[<s:ppSoapHeader xmlns:s=\"http://schemas.microsoft.com/Passport/SoapServices/SoapHeader\" version=\"1.0\"><s:lcid>1033</s:lcid><s:sitetoken><t:siteheader xmlns:t=\"http://schemas.microsoft.com/Passport/SiteToken\" id=\"" . $this->siteId . "\"/></s:sitetoken></s:ppSoapHeader>]]></ppSoapHeader25>";
			$data = $data . "     </WSSecurityHeader>";
			$data = $data . "   </soap:Header>";
			$data = $data . "   <soap:Body>";
			$data = $data . "      <GetSLT xmlns=\"http://schemas.microsoft.com/Passport/SoapServices/CredentialServiceAPI/V1\">";
			$data = $data . "         <PassIDIn>";
			$data = $data . "            <pit>PASSID_SIGNINNAME</pit>";
			$data = $data . "            <bstrID>" . $_username . "</bstrID>";
			$data = $data . "         </PassIDIn>";
			$data = $data . "         <LoginSeconds>" . $_loginSeconds . "</LoginSeconds>";
			$data = $data . "      </GetSLT>";
			$data = $data . "   </soap:Body>";
			$data = $data . "</soap:Envelope>";
			
			$tuCurl = curl_init();
			curl_setopt($tuCurl, CURLOPT_URL, $this->passportUrl);
			curl_setopt($tuCurl, CURLOPT_PORT , 443);
			curl_setopt($tuCurl, CURLOPT_VERBOSE, 1);
			curl_setopt($tuCurl, CURLOPT_HEADER, 1);
			curl_setopt($tuCurl, CURLOPT_SSLVERSION, 3);
			curl_setopt($tuCurl, CURLOPT_SSLCERT, $this->sslcert);
			curl_setopt($tuCurl, CURLOPT_SSLKEY, $this->sslkey);
			curl_setopt($tuCurl, CURLOPT_CAINFO, $this->cainfo);
			curl_setopt($tuCurl, CURLOPT_POST, 1);
			curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
			curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($tuCurl, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml","SOAPAction: \"#GetSLT\"", "Content-length: ".strlen($data)));
			$xml = curl_exec($tuCurl);		
			curl_close($tuCurl);
			
			if (  preg_match( "/<SLT>(.+)<\/SLT>/", $xml, $match_array ) == 1 ) {
	    		$slt = $match_array[1];
	  		}  		  			
			return $slt;
		}
	}
?>
<head>
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />
<title>Redirecting...GetSLT PHP v1.0.0.2</title>
</head>
<body>
<?php
	$_service = '';
	$_username = '';
	$_redirect = false;

   	// Prefix GET variables with an underscore
   	import_request_variables("g", "_");
		
	// Redirect will generally be yes
	$_redirect == "1" ? $redirect = true : $redirect = false;

	// Must have a username
	if ($_username == '') {
		die("No username passed");
	}
	
	if ($_service == '') {
		$_service = "exchangelabs";
	}
	
	// Configure it
	$getSLT = new GetSLT();
	$getSLT->siteId = "256296";
	$getSLT->sslcert = getcwd() . "/private-cer.pem";
	$getSLT->sslkey = getcwd() . "/private.pem";
	$getSLT->cainfo = getcwd() . "/all.pem";
	$slt = $getSLT->Request($_username,"30");

	$redirectUrl = '';
	
	if ($_service == "exchangelabs") {
		$redirectUrl = $getSLT->exchangeLabsUrl . "&slt=" . $slt;	
	}
		
	if ($_redirect == true) {
		header("Location: " . $redirectUrl);
	}
?>
</body>
<?php
	exit();	
	ob_end_flush();
?>